/**
 * 作者:hanbo
 * 日期:2018/12/511:32
 * 描述:包围盒
 */
import {Point} from "./Point";

export class BoundingBox {
    /**
     * 坐标值数组
     * @param array {Array} 坐标数组
     */
    constructor(array) {
        const length = array.length;
        switch (length) {
            case 2:
                this._point(array);
                break;
            case 4:
                this._polygon(array);
                break;
            default:
                break;
        }
    }

    /**
     * 点类型构造器
     * @param array {Array} 坐标数组
     * @private
     */
    _point(array) {
        this.xMin = this.xMax = array[0];
        this.yMin = this.yMax = array[0];
    }

    /**
     * 线类型构造器
     * @param array {Array} 坐标数组
     * @private
     */
    _polygon(array) {
        this.xMin = array[0];
        this.xMax = array[2];
        this.yMin = array[1];
        this.yMax = array[3];
    }

    /**
     * 添加点，自动扩大范围
     * @param point {Point} 新增点
     */
    addPoint(point) {
        if (this.xMin > point.x) {
            this.xMin = point.x
        } else if (this.xMax < point.x) {
            this.xMax = point.x
        }
        if (this.yMin > point.y) {
            this.yMin = point.y
        } else if (this.yMax < point.y) {
            this.yMax = point.y
        }
    }

    /**
     * 添加新的外接矩形扩充自己
     * @param box {BoundingBox}
     */
    addBox(box) {
        if (this.xMin > box.xMin) {
            this.xMin = box.xMin
        }
        if (this.yMin > box.yMin) {
            this.yMin = box.yMin
        }
        if (this.xMax < box.xMax) {
            this.xMax = box.xMax
        }
        if (this.yMax < box.yMax) {
            this.yMax = box.yMax
        }
    }

    /**
     * 判断两个矩形是否相交
     * @param rectangle1 {BoundingBox} 矩形1
     * @param rectangle2 {BoundingBox} 矩形2
     */
    static isIntersect(rectangle1, rectangle2) {
        if (rectangle1.xMin > rectangle2.xMax ||
            rectangle1.yMin > rectangle2.yMax ||
            rectangle2.xMin > rectangle1.xMax ||
            rectangle2.yMin > rectangle1.yMax) {
            return false
        } else {
            return true
        }
    }

    /**
     * 将矩形转换为box
     * @param rectangle {Rectangle} 传入矩形
     * @returns {BoundingBox} 包围盒
     */
    static translate(rectangle) {
        return new BoundingBox([rectangle.xMin, rectangle.yMin, rectangle.xMax, rectangle.yMax])
    }

    /**
     * 获取多段线的外接矩形
     * @param array {Array} 二维点数组[[x1,y1],[x2,y2],...]
     */
    static getPolylineBox(array) {
        let xMin, yMin, xMax, yMax;
        xMin = xMax = array[0][0];
        yMin = yMax = array[0][1];
        for (let i = 1; i < array.length; i++) {
            if (xMin > array[i][0]) {
                xMin = array[i][0]
            } else if (xMax < array[i][0]) {
                xMax = array[i][0]
            }
            if (yMin > array[i][1]) {
                yMin = array[i][1]
            } else if (yMax < array[i][1]) {
                yMax = array[i][1]
            }
        }

        return new BoundingBox([xMin,yMin,xMax,yMax])
    }

    /**
     * 获取圆形的外接矩形
     * @param center {Point} 圆形中心
     * @param option {Object} 半径
     */
    static getCircleBox(center,option){
        let radius=0;
        if(option.hasOwnProperty("radius")){
            radius=option.radius;
        }else if(option.hasOwnProperty("otherPoint")){
            radius=Point.getDistance(center,option.otherPoint)
        }
        return new BoundingBox([center.x-radius,center.y-radius,center.x+radius,center.y+radius])
    }

    /**
     *
     * @param box {BoundingBox} 包围盒
     * @param rectangle {BoundingBox} 外接矩形
     */
    static boxInExtend(box,rectangle){
        if(box.xMin>=rectangle.xMin
            &&box.xMax<=rectangle.xMax
            &&box.yMin>=rectangle.yMin
            &&box.yMax<=rectangle.yMax){
            return true;
        }
        return false;
    }
}